<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Plot extends Model
{
    protected $table = 'dd_plot';
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $guarded = array();
    /**
     * 查看小区是否存在
     * @param type $id
     * @return type
     */
    public static function hasPlot($id, $productid) {
        $product = Product::find($productid);
        $plot = static::where('area', $product->area)->where('id', $id)->first();
        return $plot ? true : false;
    }
    /**
     * 添加新小区
     * @param \Illuminate\Http\Request $request
     * @return type
     */
    public static function addPlot(\Illuminate\Http\Request $request) {
        $plot = $request->all();
        $res = static::firstOrCreate($plot);
        if ($res) {
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 1,
                'info'      => '添加成功'
            ]);
        } else {
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 0,
                'info'      => '添加失败'
            ]);
        }
    }
    /**
     * 小区优惠信息
     */
    public function strFav() {
        $favs = Plotfav::where('plotid', $this->id)->get();
        if (!count($favs)) {
            return "无优惠";
        }
        $favarr = array();
        foreach ($favs as $val) {
            $favarr[$val->productid]['plotfav'] = $val->plotfav;
            $favarr[$val->productid]['plotinit'] = $val->plotinit;
        }
        return json_encode($favarr);
    }
}
